/*
 * Copyright 2011-2020 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0.
 * See `LICENSE` in the project root for license information.
 */

package me.ijleex.mgmt.system.home.action;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;

import me.ijleex.mgmt.commons.util.MathUtils;
import me.ijleex.mgmt.framework.entity.Result;
import me.ijleex.mgmt.system.menu.service.IMenuService;
import me.ijleex.mgmt.system.user.entity.User;

/**
 * IndexAction
 *
 * @author liym
 * @since 2019-03-14 10:35 新建
 */
@Controller
public class IndexAction {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    private final IMenuService menuService;

    public IndexAction(IMenuService menuService) {
        this.menuService = menuService;
    }

    /**
     * 系统首页
     *
     * @param request 请求
     * @param model 模型数据，用于传递控制方法处理的数据到结果页面
     * @return 页面地址
     * @since 2019-03-14 10:37
     */
    @GetMapping(path = {"/", "/index"})
    public String index(HttpServletRequest request, ModelMap model) {
        // TODO 获取当前登录用户的 ID
        Long userId = 577054734542819328L;
        if (MathUtils.isNullOrZero(userId)) {
            this.logger.error("用户未登录，不能获取用户信息");
            return "login";
        }

        User user = new User(userId);
        user.setUsername("liym");
        user.setName("Lee YM");
        user.setState(0);

        String systemId = "ijleex-mgmt";
        Result menuResult = this.menuService.getMenuTreeByUser(userId, systemId);
        Object menuTree = menuResult.getMsg();

        model.put("user", user);
        model.put("menuTree", menuTree);
        model.put("copyrightYear", 2020);

        return "index";
    }

    /**
     * @since 2019-11-17 17:08
     */
    @GetMapping(path = {"/iframe/sample"})
    public String sample(HttpServletRequest request, ModelMap model) {
        model.put("copyrightYear", 2019);
        return "iframe/sample/index";
    }

    /**
     * @since 2019-11-17 17:08
     */
    @GetMapping(path = {"/iframe/sample/add"})
    public String sampleAdd(HttpServletRequest request, ModelMap model) {
        model.put("copyrightYear", 2019);
        return "iframe/sample/add";
    }

    /**
     * @since 2019-11-17 17:08
     */
    @GetMapping(path = {"/iframe/sample/update"})
    public String sampleUpdate(HttpServletRequest request, ModelMap model) {
        model.put("copyrightYear", 2019);
        return "iframe/sample/update";
    }

}
